home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / mskrmsrc.zip / MSNPKT.C < prev    next >
C/C++ Source or Header  |  1991-10-24  |  5KB  |  193 lines

  1. /* File MSNPKT.C
  2.  * Packet Driver interface
  3.  *
  4.  * Copyright (C) 1991, Trustees of Columbia University in the
  5.  *  City of New York.  Permission is granted to any individual or
  6.  *  institution to use, copy, or redistribute this software as long as
  7.  *  it is not sold for profit and this copyright notice is retained.
  8.  *
  9.  * Written for MS-DOS Kermit by Joe R. Doupnik, 
  10.  *  Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet
  11.  *
  12.  * Last edit
  13.  * 6 Sept 1991
  14.  */
  15. #include "msntcp.h"
  16. #include "msnlib.h"
  17.  
  18. #define BUFSIZE    3000            /* size of pkt receive buffer */
  19.  
  20. word    pkt_ip_type = 0x0008;        /* these are little endian values */
  21. word    pkt_arp_type = 0x0608;
  22. word    pkt_rarp_type = 0x3580;
  23.  
  24. word    pkt_ip_handle = -1;        /* -1 means invalid handle */
  25. word    pkt_arp_handle = -1;
  26. word    pkt_rarp_handle = -1;
  27.  
  28. int    pdversion, pdtype, pdnum, pdfunct;
  29. extern    word pktdevclass;
  30. extern    int pdinit(), pdinfo(), pdaccess(), pdclose();
  31. extern    eth_address eth_addr;
  32.  
  33. static byte pktbuf[BUFSIZE] = {0}; /* linked list packet receive buffer */
  34. static byte * pktbuf_read = pktbuf;
  35. byte pktwnum = 0;        /* seq number for packets written to buffer */
  36. byte pktrnum = 0;        /* seq number for packets read from buffer */
  37. byte * pktbuf_wrote = &pktbuf[BUFSIZE - 4];
  38.  
  39.  
  40. int
  41. pkt_eth_init()
  42. {
  43.     
  44.         pkt_buf_wipe();        /* clean out and init receiver buffer */
  45.     if (pdinit(pktbuf, eth_addr) == 0)
  46.             {
  47.         outs("\r\nCannot attach to an Ethernet Packet Driver");
  48.         return( 0 );
  49.         }
  50.     pkt_buf_wipe();
  51.                     /* lets find out about the driver */
  52.     if ((pdinfo(&pdversion, &pktdevclass, &pdtype, &pdnum, &pdfunct))
  53.         == 0)
  54.         {
  55.         outs("\r\nCannot obtain Packet Driver information");
  56.         return (0);
  57.         }
  58.     if (pdaccess(&pkt_arp_type, sizeof(pkt_arp_handle), &pkt_arp_handle)
  59.         == 0)
  60.          {
  61.         outs("\r\n\7Cannot access ARP type packets");
  62.         return (0);
  63.         }
  64.     if (pdaccess(&pkt_ip_type, sizeof(pkt_ip_handle), &pkt_ip_handle)
  65.         == 0)
  66.          {
  67.         outs("\r\n\7Cannot access IP type packets");
  68.         return (0);
  69.         }
  70.     if (pdaccess(&pkt_rarp_type, sizeof(pkt_rarp_handle), &pkt_rarp_handle)
  71.         == 0)
  72.          {
  73.         outs("\r\n\7Cannot access RARP type packets");
  74.         return (0);
  75.         }
  76.     return (1);                /* say success */
  77. }
  78.  
  79. int
  80. pkt_release()
  81. {
  82.     register int status = 1;            /* assume success */
  83.  
  84.     if ( pktdevclass == PD_SLIP ) return (0);
  85.  
  86.         if (pkt_arp_handle != -1)
  87.         if (pdclose(pkt_arp_handle) == 0)
  88.             {
  89.             outs("\r\nERROR releasing Packet Driver for ARP");
  90.             status = 0;
  91.             }
  92.         else pkt_arp_handle = -1;    /* handle is out of service */
  93.  
  94.     if (pkt_ip_handle != -1)
  95.         if (pdclose(pkt_ip_handle) == 0)
  96.             {
  97.             outs("\r\nERROR releasing Packet Driver for IP");
  98.             status = 0;
  99.             }
  100.         else pkt_ip_handle = -1;    /* handle is out of service */
  101.  
  102.     if (pkt_rarp_handle != -1)
  103.         if (pdclose(pkt_rarp_handle) == 0)
  104.             {
  105.             outs("\r\nERROR releasing Packet Driver for RARP");
  106.             status = 0;
  107.             }
  108.         else pkt_rarp_handle = -1;    /* handle is out of service */
  109.     return (status);
  110. }
  111.  
  112. /* Deliver pointer to start of packet (destination address) or NULL.
  113.  * Simple linked list: byte flag, byte pkt seq number, int count, 
  114.  * byte data[count] and so on.
  115.  * Status on the link flag byte is
  116.  * 0 = end of buffer (count has size to point to start of buffer)
  117.  * 1 = this slot is free (unused)
  118.  * 2 = this slot has an unread packet
  119.  * 4 = this slot is allocated for a packet, packet not yet loaded into it
  120.  * 8 = this slot has a packet which has been read once
  121.  * A two byte count value follows the flag byte, for number of bytes in 
  122.  * this slot. Pktbuf_read remembers the pointer to the last-read slot.
  123. */
  124.  
  125. void * pkt_received()
  126. {
  127.     register byte * p;
  128.     register int i;
  129.  
  130.     p = pktbuf_read;            /* start with last read */
  131.     for (i = 0; i < 2 * (BUFSIZE / (60 + 4)); i++)    /* 2 * max pkts */
  132.         {
  133. /* if link is:    end of buf    free     allocated   have read */
  134.         if (*p == 0 || *p == 1 || *p == 4 || *p == 8)
  135.             {
  136.             p += 4 + *(word *)(p+2);   /* point at next link */
  137.             continue;
  138.             }
  139.         if (*p == 2)             /* ready to be read */
  140.             {
  141.             if (*(p+1) == pktrnum)    /* if this is the next pkt */
  142.                 {
  143.                  pktbuf_read = p;    /* where we read */
  144.                 *p = 8;            /* mark as read */
  145.                 pktrnum++;        /* next one to read */
  146.                 return (p + 4);        /* return ptr to pkt*/
  147.                 }
  148.             else
  149.                 {
  150.                 p += 4 + *(word *)(p+2); /* next link */
  151.                 i = 1;            /* restart counter */
  152.                 continue;
  153.                 }
  154.             }
  155.         else
  156.             {
  157.             pkt_buf_wipe();        /* emergency treatment */
  158.             break;
  159.             }
  160.         }
  161.     return (NULL);
  162. }
  163.     
  164. void
  165. pkt_buf_release(byte *p)    /* return a buffer to the pool */
  166. {
  167.     disable();        /* disallow interrupts here */
  168.     p -= 4 + 6 + 6 + 2;    /* backup from IP header to link start */
  169.  
  170.     if (*p == 8)        /* if packet has been read */
  171.         *p = 1;        /* mark link as freed */
  172.     enable();
  173. }
  174.  
  175. void
  176. pkt_buf_wipe()                    /* clear all buffers */
  177. {
  178.     disable();
  179.     pktbuf[0] = 1;                /* flag first link as free */
  180.     pktbuf[1] = 0;
  181.     *(word *)&pktbuf[2] = BUFSIZE - 8; /* free space, size - two links */
  182.  
  183.     pktbuf[BUFSIZE - 4] = 0;        /* flag as end of buffer */
  184.     pktbuf[BUFSIZE - 3] = 0;        /* pkt buffer seq number */
  185.                 /* count below points to start of buffer */
  186.     *(word *)&pktbuf[BUFSIZE - 2] = - BUFSIZE; 
  187.     pktbuf_read = pktbuf;                /* where last read */
  188.     pktbuf_wrote = &pktbuf[BUFSIZE - 4];        /* where last wrote*/
  189.     pktwnum = pktrnum = 0;        /* reset buffer sequence numbers */
  190.     enable();
  191. }
  192.  
  193.